home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / PROBLEMS / ASM / SQUAREROOT < prev    next >
Internet Message Format  |  1991-10-06  |  2KB

  1. From: chughes@maths.tcd.ie (Conrad Hughes)
  2.  
  3. >WANTED: 32/64 bit square root algorithms in Assembly.
  4. Here's the best code I've managed yet; it manages to fit into four
  5. instructions per bit. If anyone has managed better, please let me
  6. know.. If you do use the code, it'd be nice to receive a mention
  7. :-)
  8. DEFFNsqrt(N,R,T,S):LOCALA%:IFS<>N [OPTpass:MOV S,N:]
  9. [OPTpass:MOV R,#0:]:FORA%=7TO0STEP-1:[OPTpass:ADD T,R,#1<<A%+15
  10. CMP S,T,LSL#A%+1:SUBGE S,S,T,LSL#A%+1:ADDGE R,R,#1<<A%+16:]NEXT:[OPTpass
  11. ADD T,R,#1<<14:CMP S,T:SUBGE S,S,T:ADDGE R,R,#1<<15:]FORA%=2TO15:[OPTpass
  12. ADD T,R,#1<<15-A%:RSBS S,T,S,LSL#1:ADDLT S,S,T:ADDGE R,R,#1<<16-A%:]NEXT
  13. [OPTpass:ADD T,R,R:ADD T,T,#1:RSBS S,T,S,LSL#2:ADDLT S,S,T:ADDGE R,R,#1
  14. CMP S,R:ADDGE R,R,#1:]=0
  15. ..defines a function which calculates the square root of a number
  16. stored in one register, with the fixed point between bits 15 and 16;
  17. I didn't write down behaviour for negative numbers, but you should
  18. be able to test this out for yourself, and it shouldn't be too
  19. difficult to extend to different formats. To convert a number into
  20. this format, you just multiply it by 65536, and to convert back, you
  21. divide by 65536 (i.e., 1.000 would be represented as 65536 in a
  22. register).
  23.  
  24. So, for example, you want R1=SQRT(R0):
  25. FNsqrt(0,1,2,3) will preserve the contents of R0, and use R2 and R3
  26. for data - both will be corrupted.
  27. FNsqrt(0,1,2,0) will corrupt R0, but doesn't need R3.
  28. There are no other circumstances under which two of the registers
  29. the routine uses can safely be the same.
  30.  
  31. The loops are unwrapped for speed (actually using loops would take
  32. up much less space, but would probably use up another register, and
  33. make the routine twice as slow). The routine takes up 400 or 404
  34. bytes, depending on whether N=S or not.
  35.  
  36. One thing - this is slow :-}, so wherever possible you should avoid
  37. using square roots.. Similarly division.
  38.  
  39.  
  40.